#include <time.h>		//For Date fetching/formatting

#include "MusinkSubmitter.h"

using namespace std;	//Namespace hack

MusinkSubmitter sub;	//Submitter singleton (no need to open multiple connections to DBM)

void initialize()
{
	sub = MusinkSubmitter(); //create new MusinkSubmitter instance.
}

void initialize(string id)
{
	sub = MusinkSubmitter(id); //create new MusinkSubmitter instance.
}

void newPlayerStartup()
{
	initialize();				//Initialize player
	string pluginID = sub.getID();	//Get the plugin ID returned from the server
	//Write out the pluginID to a file


	vector<string> filenames;	//Filenames of songs in Player library (full path);
	
	vector<MusinkSong> library = sub.songCollection(filenames);	//Get MusinkSong objects (i.e. hash data) from Database Manager
	vector<MusinkSong> conflicts;
	vector<MusinkSong> conflictUpdates;

	unsigned int i;			//Loop variables

	for (i=0; i<library.size(); ++i)
	{
		/*
			For each song in library:
				Update song.artist, album, title, duration, filePath to match player[song] info
					->  song.hash should remain unchanged
				song.rating = player[song].rating;		//Use Player's rating so we check for rating conflicts
														//Keep in mind, Player Rating needs to be converted to x/10.0


				song.playCount += player[song].playCount;		//Make sure playCount matches in both places
				player[song] = song.playCount;

				if (song.lastPlayed < player[song.lastPlayed)	//Give the song object the Newer of the two dates
					song.lastPlayed = player[song].lastPlayed;  //Note should be string in format YYYYMMDD HH:mm:ss , YYYY = #### MM = 00-12, DD=00-30, HH =00-23 mm=00-59, ss=0-59
		*/
	}
	
	sub.syncDatabases(library,conflicts);		//library and conflicts will be returned with MusinkSong Objects with everything matching except .rating
												//Only Songs which had conflicts will still be in library.  All others have been removed

	for (i=0; i<library.size(); ++i)
	{
		/*		
			For each song in Library and Conflicts
			Present User with a Dialog saying the following
				Song "<song.filePath>" has a rating conflict:
				<library[i].artist> - <library[i].album> - <library[i].title>
					<PlayerName> rating: <library[i].rating>
					Musink rating: <conflicts[i].rating>

				Which rating should be used?
					<PlayerName>  --> conflictUpdates.push_back(library[i]);
					Musink		  --> conflictUpdates.push_back(conflicts[i]);
					<PlayerName> / Musink All and code as appropriate
					
		*/
	}

		//Submit song to DatabaseManager
	for (i=0; i<conflictUpdates.size(); ++i)
	{
		sub.updateSong(conflictUpdates[i]);
	}

	
}

void oldPlayerStartup(string id)
{
	//recover plugin ID from file
	initialize(id);	//Initialize MusinkSubmitter with the ID

	vector<MusinkSong> changes;
	sub.getChanges(changes);		// Get a vector of MusinkSong Objects that have changed in other players 
									// since this player was last launched ("changes")
									//
									// This means that the Player library should have it's rating, 
									// playcount, and last played statistics set equal to these files

	for (unsigned int i=0; i<changes.size(); ++i)
	{
		/*
			lSong = playerLib.getSongByFileName(changes[i].filePath);
			lSong.rating = changes[i].rating; //Note: Rating is out of 10.0
			lSong.playCount = changes[i].rating; //Since we increment playCount by 1 each time it's played, there is no need to update this.
			
		*/
	}
	
}

void incrementSongPlays(string fileName)
{
	MusinkSong s = sub.getSong(fileName);
	s.playCount++;

	time_t rawtime;
	time(&rawtime);
	struct tm* timeinfo = localtime(&rawtime);
	char tdate[80];
	
	strftime(tdate,80,"%y%m%d %H:%M:%S",timeinfo);
	s.lastPlayed = tdate;
}

void changeSongRating(string fileName, double rating, double totalPossible)
{
	double trueRating = (rating/totalPossible)*10.0;
	MusinkSong s = sub.getSong(fileName);
	s.rating = trueRating;
	sub.updateSong(s);

}

void otherSongUpdates(string fileName)
{
	MusinkSong song = sub.getSong(fileName);
	//Change appropriate information in song
		//song.artist = "Phish"
	sub.updateSong(song);
}